package webService.DAO;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import webService.entities.Transaction;

public class TransactionDAO {

	//@PersistenceContext(unitName = "JPABANKDB")
	private EntityManager em;
	
	public void create(Transaction transaction)
	{
		em.persist(transaction);
		em.flush();
		
	}
	
	public Transaction update(Transaction transaction)
	{
		return em.merge(transaction);
		
	}
	
	public void delete(Transaction transaction)
	{
		Transaction toRemoved = em.merge(transaction);
		em.remove(toRemoved);
	}
	
	public Transaction find(String uuidTransaction) 
	{
		Transaction result = null;
		String namedQuery = "SELECT distinct T from Transaction T " +
				" where T.uuidTransaction = :param " ;
		try {
			Query query = em.createNamedQuery(namedQuery);

			query.setParameter("param", uuidTransaction);

			result = (Transaction) query.getSingleResult();

		} catch (Exception e) {
			// todo
			e.printStackTrace();
		}

		return result;
	}
	
	
}
